import axios from 'axios'
import { ElMessage } from 'element-plus'
import store from '@/store'
import storageHelper from '@/libs/StorageHelper'
let token=storageHelper.getToken()||''
console.log(token)
class RequestHelper {
  constructor() {
    this.baseURL = import.meta.env.VITE_WEB_BASE_URL
  }
  // 请求
  request(options) {
    // console.log(options)
    let instance = axios.create()
    let opt = Object.assign(this.getDefaultConfig(), options)
    this.interceptors(instance)
    return instance(opt)
  }
  // 默认配置
  getDefaultConfig() {
    let config = {
      baseURL: this.baseURL,
      timeout: 30000,
      headers: {},
    }
    return config
  }

  // 拦截器
  interceptors(instance) {
    instance.interceptors.request.use(
      (config) => {
        //store.commit('setLoading', 'create')
        if (config.url.includes('/api/user/face_login')||config.url.includes('/api/user/upload')) {
         
          //登录接口不需要token,不然后端会报401
          delete config.headers.Authorization
        } else {
          config.headers['X-Auth-Token'] =storageHelper.getToken()
        }
      return config
      },
      (error) => {
        console.log(error)
        return Promise.reject(error)
      }
    )

    instance.interceptors.response.use(
      (response) => {
        // store.commit('setLoading', 'finish')
        // 特殊处理流文件
        // let url = response.config.url
        // if (url.includes('/product/location/import/excel')) {
        //   return response
        // }
        // console.log('response', response)

        switch (response.data.code) {
          case 400:
            ElMessage.error(response.data.msg || '操作失败')
            break
          case 401:
            ElMessage.error(response.data.msg || '操作失败')
            break
          case 403:
            ElMessage.error(response.data.msg || '用户登录过期')
            // storageHelper.clearAll()
            setTimeout(() => {
              location.reload()
            }, 100)
            break
          case 500:
            ElMessage.error(response.data.msg || '操作失败')
            break
          case 503:
            ElMessage.error(response.data.msg || '网络错误')
            break
        }
        return response.data
      },
      (error) => {
        ///console.log('error', error)
        // store.commit('setLoading', 'finish')
        // if (error.code === 'ECONNABORTED') {
        //   // return ElMessage.error('请求超时请刷新页面重试！')
        // }
        // if (error.code === 'ERR_NETWORK') {
        //   return ElMessage.error(error.message)
        // }
        if (error.response) {
          switch (error.response.status) {
            case 500:
              ElMessage.error(`服务器错误${error.message}`)
              break
            case 503:
              ElMessage.error(`服务器错误${error.message}`)
              break
          }
        } else {
          return ElMessage.error(`请求无响应`)
        }
        return Promise.reject(error)
      }
    )
  }

  get({ url, data, options = {} }) {
    if (data) {
      options.url = `${url}\\${data}`
    } else {
      options.url = url
    }
    options.method = 'get'
    options.headers = {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    }
    return this.request(options)
  }

  getByParams({ url, data, options = {} }) {
    options.url = url
    options.params = data
    options.method = 'get'
    options.headers = {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
      'X-Auth-Token': storageHelper.getToken()
    }
    return this.request(options)
  }

  post({ url, data, options = {} }) {
    options.url = url
    options.data = data
    options.method = 'post'
    return this.request(options)
  }

  postByParams({ url, data, options = {} }) {
    options.url = url
    options.params = data
    options.method = 'post'
    return this.request(options)
  }

  put({ url, data, options = {} }) {
    options.url = url
    options.data = data
    options.method = 'put'
    return this.request(options)
  }

  delete({ url, data, options = {} }) {
    if (data) {
      options.url = `${url}\\${data}`
    } else {
      options.url = url
    }
    options.method = 'delete'
    options.headers = {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    }
    return this.request(options)
  }

  deleteByBody({ url, data, options = {} }) {
    options.url = url
    options.data = data
    options.method = 'delete'
    return this.request(options)
  }
}
let requestHelper = new RequestHelper()

export default requestHelper
